package com.ftu.study.pattern.state;

import com.ftu.study.pattern.state.impl.HasQuarterState;
import com.ftu.study.pattern.state.impl.NoQuarterState;
import com.ftu.study.pattern.state.impl.SoldOutState;
import com.ftu.study.pattern.state.impl.SoldState;

/**
 * Created by zhanghr on 2018/12/22.
 */
public class GumballMachine2 {
    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;

    State state = soldOutState;
    int count = 0;

    public GumballMachine2(int count) {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        this.count = count;
        if (count > 0) {
            state = noQuarterState;
        }
    }

    public void insertQuarter() {
        state.insertQuarter();
    }

    public void ejectQuarter() {
        state.ejedtQuarter();
    }

    public void turnCrank() {
        state.turnCrank();
    }

    public GumballMachine2 setState(State state) {
        this.state =state;
        return this;
    }

    public void releaseBall() {
        System.out.println("A gumball comes rolling out the slot...");
        if(count != 0) {
            count--;
        }
    }

    @Override
    public String toString() {
        return "GumballMachine: state: "+ state +" count: "+ count +"";
    }
}
